﻿using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Threading.Tasks;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Live;
using Microsoft.Live.Controls;

// SkyDrive session
namespace GenetecHackathonCod3
{
    
    public partial class EditReport : PhoneApplicationPage
    {

        private LiveConnectClient client;
        public static Report currentReport;
        
        List<Note> notes;
        List<Recording> recordings;

        public static int selected;

        public EditReport()
        {
            InitializeComponent();
            currentReport = MainPage.reports.ElementAt(MainPage.selected);
            reportName.Title = currentReport.getTitle();
            Title.Text = currentReport.getTitle();
            notes = currentReport.getNotes();
            recordings = currentReport.recordings;
            populateList();
        }

        void NewReport_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/NewReport.xaml", UriKind.Relative));
        }

        private void TakePicture_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/TakePicture.xaml", UriKind.Relative));
        }
        private void TakeSound_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/RecordingAudio.xaml", UriKind.Relative));
        }

        private void RecordVideo_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/RecordingVideo.xaml", UriKind.Relative));
        }

        void SaveReport_Click(object sender, EventArgs e)
        {
            currentReport.setTitle(Title.Text);
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        void SendReport_Click(object sender, EventArgs e)
        {

        }
        private void Title_LostFocus(object sender, RoutedEventArgs e)
        {
           currentReport.setTitle(Title.Text);
        }

        private void NoteList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (NoteList.SelectedIndex != -1)
            {

                selected = NoteList.SelectedIndex;

                NavigationService.Navigate(new Uri("/ViewNote.xaml", UriKind.Relative));

                NoteList.SelectedIndex = -1;
            }
        }

        private void populateList()
        {

            foreach (Note note in notes)
            {
                TextBlock item = new TextBlock();
                item.Text = "" + note.getTitle() + "\n" + note.getDateString();
                NoteList.Items.Add(item);
            }

            foreach (Recording recording in recordings)
            {
                TextBlock item = new TextBlock();
                item.Text = "" + recording.sound + "\n" + recording.getDateString();
                AudioList.Items.Add(item);
            }


        }

        private void NewNote_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/NewNote.xaml", UriKind.Relative));
        }


        private void AudioList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (AudioList.SelectedIndex != -1)
            {

                selected = AudioList.SelectedIndex;

                RecordingAudio.playSound(recordings.ElementAt(selected).sound);

                AudioList.SelectedIndex = -1;
            }
        }

        private void skydrive_SessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)

{
    if (e.Status == LiveConnectSessionStatus.Connected)
    {
        client = new LiveConnectClient(e.Session);
        MessageBox.Show("Connected!");
        }
}

        private void UploadFile_Click(object sender, RoutedEventArgs e)
        {
            client.UploadCompleted += client_UploadCompleted;
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsolatedStorageFileStream stream = storage.OpenFile("/Images/add.png", FileMode.Open,FileAccess.Read);
                client.UploadAsync("Users/David/skydrive", "SpeakerTimer.bak", stream, OverwriteOption.Overwrite);
            }
        }

        void client_UploadCompleted(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Upload successfull!");
            }
        }


    }
}